﻿using System.IO;
using System.Text;
using TemplateMigrator.Application;

namespace TemplateMigrator.Present.Console
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var originalSourceTemplatePath = "";
            var newSolutionBasePath = "";
            var projectStringToReplace = "";
            var projectReplacementString = "";
            var replaceGuids = true;
            var foundHelpVariable = false;

            if (args.Length > 0)
            {
                foreach (var arg in args)
                {
                    var split = arg.Split(':');

                    if (split.Length == 1)
                    {
                        if (split[0].Trim() == "/help")
                        {
                            WriteHelpMessage();
                            System.Console.Read();
                            foundHelpVariable = true;
                        }
                    }
                    if (!foundHelpVariable)
                    {
                        // NOTE: Will fail if there is a colon (:) in the value section
                        if (split.Length > 1)
                        {
                            var key = split[0];
                            var value = split[1].Trim();

                            switch (key.Replace("/", ""))
                            {
                                case "sourceDir":
                                case "sd":
                                    originalSourceTemplatePath = value;
                                    break;

                                case "newDir":
                                case "nd":
                                    newSolutionBasePath = value;
                                    break;

                                case "oldString":
                                case "os":
                                    projectStringToReplace = value;
                                    break;
                                case "newString":
                                case "ns":
                                    projectReplacementString = value;
                                    break;

                                case "replaceGuid":
                                case "r":
                                case "rg":
                                    var rg = value;
                                    if (!bool.TryParse(rg, out replaceGuids))
                                    {
                                        replaceGuids = true;
                                    }

                                    break;
                            }
                        }
                    }
                }
            }
             ///ONLY USED FOR INITIAL TESTING
            else
            {
                // TODO: Replace temporary with other means of grabbing creation 
                //          information

                originalSourceTemplatePath = @"C:\projects\EWFS\I9Poc\EWFS.Employee";
                newSolutionBasePath = @"C:\projects\EWFS\EWFS.I9.Section1";
                projectStringToReplace = "Employee";
                projectReplacementString = "I9.Section1";
                replaceGuids = true;
            }


            if (!FilesAreValid(originalSourceTemplatePath
                , projectStringToReplace, projectReplacementString))
            {
                // TODO: Make this more granular
                System.Console.WriteLine("Missing information to run program");
            }
            else if (!foundHelpVariable)
            {
                RunDirectory(originalSourceTemplatePath,
                    newSolutionBasePath, projectStringToReplace,
                    projectReplacementString, replaceGuids);
            }
        }

        private static void WriteHelpMessage()
        {
            var builder = new StringBuilder();
            builder.Append("Usage: TemplateMigrator.exe /sd: {source directory (template)}  /nd: {new directory}");
            builder.Append(
                "       /os: {old string (name to replace in template)} /ns: {string that replaces old string}");
            builder.Append("       /rg: {replace the GUIDs [TRUE|FALSE] - should GUIDs be replaced (default TRUE)}");
            builder.Append("\n\r");

            System.Console.WriteLine(builder.ToString());
        }

        private static bool FilesAreValid(string originalSourceTemplatePath
            , string projectStringToReplace, string projectReplacementString)
        {
            var areValid = true;

            if (!Directory.Exists(originalSourceTemplatePath))
            {
                areValid = false;
            }

            if ((projectStringToReplace.Trim().Length == 0) ||
                (projectReplacementString.Trim().Length == 0))
            {
                areValid = false;
            }

            return areValid;
        }

        private static void RunDirectory(string originalSourceTemplatePath, string newSolutionBasePath
            , string projectStringToReplace, string projectReplacementString, bool replaceGuids = true)
        {
            var migrator = new Migrator(originalSourceTemplatePath
                , newSolutionBasePath, projectStringToReplace
                , projectReplacementString, replaceGuids);

            migrator.MigrateTemplate();
        }
    }
}